﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace QWeiBoN.Util
{
    /// <summary>
    /// 文件上传工具类
    /// </summary>
    public class UploadFileUtil
    {

        /// <summary>
        /// 文件名称
        /// </summary>
        public string FileName { get; private set; }

        /// <summary>
        /// 文件类型
        /// </summary>
        public string ContentType { get; private set; }

        /// <summary>
        /// 本地文件路径
        /// </summary>
        private string FilePath { get; set; }
        /// <summary>
        /// 文件数据流
        /// </summary>
        public Stream FileStream { get; private set; }

        /// <summary>
        /// 文件流实现
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="stream">文件流</param>
        public UploadFileUtil(string fileName, Stream stream)
        {
            this.FileName = System.IO.Path.GetFileName(fileName);
            this.ContentType = GetContentType(this.FileName);
            this.FileStream = stream;
        }

        /// <summary>
        /// 将文件写入文件流
        /// </summary>
        /// <param name="stream"></param>
        public void WriteStream(Stream stream)
        {
            byte[] buffer = new byte[512];
            int size = 0;
            if (null != this.FileStream)
            {
                while ((size = this.FileStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    stream.Write(buffer, 0, size);
                }
            }
            if (!string.IsNullOrEmpty(this.FilePath)
                && File.Exists(this.FilePath))
            {
                //写入本地文件流
                using (System.IO.FileStream reader = new FileStream(this.FilePath, FileMode.Open, FileAccess.Read))
                {
                    while ((size = reader.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        stream.Write(buffer, 0, size);
                    }
                }
            }
        }

        /// <summary>
        /// 通过文件名得到文件类型
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static string GetContentType(string fileName)
        {
            var file = System.IO.Path.GetExtension(fileName);
            return GetFileContentType(file);
        }

        /// <summary>
        /// 得到文件类型
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private static string GetFileContentType(string file)
        {
            switch (file)
            {
                case ".jpg":
                case ".jpeg":
                    return "image/jpeg";
                case ".gif":
                    return "image/gif";
                case ".bmp":
                    return "image/bmp";
                case ".png":
                    return "image/png";
                default:
                    return "application/octetstream";
            }
        }
    }
}
